<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-21
 * Time: 下午8:21
 */

namespace Application\Base;

class ctlBase
{
    /**
     * 项目名称,Application文件夹下的项目名称，如Base
     * @var string
     */
    public $application = "";

    /**
     * 项目下控制层文件的标识
     * @var string
     */
    public $module = "";

    /**
     * 控制层中的方法名称
     * @var string
     */
    public $method = "";

    /**
     * 访问的URL地址
     * @var string
     */
    public $url = "";

    public $typeInt = \LibHttps::TYPE_INT;

    public $typeString = \LibHttps::TYPE_STRING;

    public function __construct()
    {

    }

    public function init($application, $module, $method, $url)
    {
        $this->application = $application;
        $this->module = $module;
        $this->method = $method;
        $this->url = $url;
        $this->base();
    }

    public function base()
    {

    }

    public function assign($key, $value = "")
    {
        if (!is_array($key)) {
            $datas[$key] = $value;
        } else {
            $datas = $key;
        }
        /**
         * @var \LibTemplate $libTemplate
         */
        $libTemplate = \LibTemplate::getInstance();
        $libTemplate->assign($datas);
    }

    /**
     * 渲染模板
     * @param string $template
     * @param bool $return
     * @return string
     */
    public function display($template = "", $return = false)
    {
        if (!$template) {
            $template = $this->module . "_index";
        }
        /**
         * @var \LibTemplate $libTemplate
         */
        $libTemplate = \LibTemplate::getInstance();
        /**
         * @var \Library\template\LibParse $libParse
         */
        $libParse = \Library\template\LibParse::getInstance();
        $libParse->getStaticList($this->application, $template);
        if (\Library\template\LibTools::isSyncTemplate($this->application, $template)) {
            $libTemplate->compile($this->application, $template);
        }
        return $libTemplate->display($this->application, $template, $return);
    }

    /**
     * 返回模板里面的内容
     * @param $templage
     * @return string
     */
    public function sdisplay($templage)
    {
        return $this->display($templage, true);
    }

    /**
     * get请求
     * @param $param
     * @param string $type
     * @return mixed
     */
    public function get($param, $type = "")
    {
        /**
         * @var  \LibHttps $libHttps
         */
        $libHttps = \LibHttps::getInstance();
        return $libHttps->get($param, $type);
    }

    /**
     * post请求
     * @param $param
     * @param string $type
     * @return mixed
     */
    public function post($param, $type = "")
    {
        /**
         * @var  \LibHttps $libHttps
         */
        $libHttps = \LibHttps::getInstance();
        return $libHttps->post($param, $type);
    }

    public function request($param, $type = "")
    {

    }

    /**
     * ajax返回数据的方法
     * @param $code
     * @param array $result
     * @param array $datas
     * @return bool
     */
    public function response($code, $result = [], $datas = [])
    {
        $o = new \stdClass();
        $o->code = $code;
        $o->result = $result;
        if (is_array($datas)) {
            foreach ($datas as $key => $data) {
                $o->$key = $data;
            }
        } else {
            $o->msg = $datas;
        }
        if ($code == 100) {
            $o->success = true;
        } else {
            $o->success = false;
        }
        $s = json_encode($o);
        echo $s;
        return $o->success;
    }
} 